package org.jeecg.modules.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.model.EnumDictOption;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppClientMenu;
import org.jeecg.modules.app.entity.AppMenu;
import org.jeecg.modules.app.service.IAppClientMenuService;
import org.jeecg.modules.app.service.IAppClientService;
import org.jeecg.modules.app.service.IAppMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 应用管理
 * @Author: sean
 * @Date:   2024-09-30
 * @Version: V1.0
 */

@Slf4j
@Api(tags="应用管理")
@RestController
@RequestMapping("/app/client")
@RequiredArgsConstructor
public class AppClientController extends JeecgController<AppClient, IAppClientService> {

	private final IAppClientService appClientService;
	private final IAppClientMenuService appClientMenuService;
	private final IAppMenuService appMenuService;
	
	/**
	 * 分页列表查询
	 * @param appClient
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//@AutoLog(value = "应用管理-分页列表查询")
	@ApiOperation(value="应用管理-分页列表查询", notes="应用管理-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="app/client/list")
	public Result<IPage<AppClient>> queryPageList(AppClient appClient, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
												  HttpServletRequest request) {
		QueryWrapper<AppClient> queryWrapper = QueryGenerator.initQueryWrapper(appClient, request.getParameterMap());
		Page<AppClient> page = new Page<>(pageNo, pageSize);
		IPage<AppClient> pageList = appClientService.selectJoinPage(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 列表查询
	 * @param appClient
	 * @return
	 */
	@ApiOperation(value="应用管理-列表查询", notes="应用管理-列表查询")
	@GetMapping(value = "/options")
	public Result<List<EnumDictOption>> queryPageList(AppClient appClient) {
		List<EnumDictOption> list = appClientService.selectOption(appClient);
		return Result.OK(list);
	}
	
	/**
	 * 添加
	 * @param appClient
	 * @return
	 */
	@AutoLog(value = "应用管理-添加")
	@ApiOperation(value="应用管理-添加", notes="应用管理-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AppClient appClient) {
		appClientService.save(appClient);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 * @param appClient
	 * @return
	 */
	@AutoLog(value = "应用管理-编辑")
	@ApiOperation(value="应用管理-编辑", notes="应用管理-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AppClient appClient) {
		appClientService.updateById(appClient);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "应用管理-通过id删除")
	@ApiOperation(value="应用管理-通过id删除", notes="应用管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		appClientService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "应用管理-批量删除")
	@ApiOperation(value="应用管理-批量删除", notes="应用管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.appClientService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "应用管理-通过id查询")
	@ApiOperation(value="应用管理-通过id查询", notes="应用管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AppClient> queryById(@RequestParam(name="id",required=true) String id) {
		AppClient appClient = appClientService.getById(id);
		if(appClient==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(appClient);
	}

    /**
    * 导出excel
    * @param request
    * @param appClient
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AppClient appClient) {
        return super.exportXls(request, appClient, AppClient.class, "应用管理");
    }

    /**
    * 通过excel导入数据
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AppClient.class);
    }


	/**
	 * 查询应用菜单
	 * @return
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.GET)
	public Result<List<String>> menus(@RequestParam(name = "code", required = true) String code,
									  @RequestParam(name = "platformType", required = true) String platformType) {
		Result<List<String>> result = new Result<>();
		try {
			AppClient appClient = appClientService.getOne(new QueryWrapper<AppClient>().eq("code", code).eq("platform_type", platformType));
			if(ObjectUtil.isNotEmpty(appClient)){
				List<AppClientMenu> list = appClientMenuService.list(new QueryWrapper<AppClientMenu>().lambda().eq(AppClientMenu::getClientId, appClient.getId()));
				if(ObjectUtil.isNotEmpty(list)){
					Set<Long> menuIds = list.stream().map(AppClientMenu::getMenuId).collect(Collectors.toSet());
					List<AppMenu> appMenuList = appMenuService.listByIds(menuIds);
					result.setResult(appMenuList.stream().map(AppMenu -> String.valueOf(AppMenu.getCode())).collect(Collectors.toList()));
				}
			}
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return result;
	}


}
